package com.example.demo1122.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo1122.domain.LoginStudent;
import com.example.demo1122.entity.Student;
import com.example.demo1122.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class StudentUserDetailsService implements UserDetailsService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<Student> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentID,username);
        Student student=studentMapper.selectOne(queryWrapper);
        //判断是否为空
        if(Objects.isNull(student)){
            throw new RuntimeException("用户名或密码错误");
        }
        //TODO 根据用户查询权限信息 添加到LoginUser中

        //封装成UserDetails对象返回
        return new LoginStudent(student);

    }
}
